const CryptoJS = require('crypto-js')
module.exports = {
	success(data = {}) {
		const body = {
			success: true,
		}
		if (data) {
			body.data = data
		}
		this.ctx.body = body
	},
	error(error, status) {
		this.ctx.status = status || 403
		this.ctx.body = {
			success: false,
			data: error,
		}
	},
	getToken(opt) {
		return this.app.jwt.sign(opt, this.app.config.jwt.secret, {
			expiresIn: '7d',
		})
	},
	// 加密
	encrypt(word, keyStr, ivStr) {
		let key = this.config.KEY
		let iv = this.config.IV
		if (keyStr) {
			key = CryptoJS.enc.Utf8.parse(keyStr)
			iv = CryptoJS.enc.Utf8.parse(ivStr)
		}
		let srcs = CryptoJS.enc.Utf8.parse(word)
		var encrypted = CryptoJS.AES.encrypt(srcs, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.ZeroPadding,
		})
		return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
	},
	// 解密
	decrypt(word, keyStr, ivStr) {
		let key = this.config.KEY
		let iv = this.config.IV

		if (keyStr) {
			key = CryptoJS.enc.Utf8.parse(keyStr)
			iv = CryptoJS.enc.Utf8.parse(ivStr)
		}

		let base64 = CryptoJS.enc.Base64.parse(word)
		let src = CryptoJS.enc.Base64.stringify(base64)

		let decrypt = CryptoJS.AES.decrypt(src, key, {
			iv: iv,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.ZeroPadding,
		})

		let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
		return decryptedStr.toString()
	},
	toTree(array) {
		const parent = [],
			child = []
		array.forEach(element => {
			if (element.pid === 0) {
				element.children = []
				parent.push(element)
			} else {
				child.push(element)
			}
		})
		for (let i = 0; i < child.length; i++) {
			for (let j = 0; j < parent.length; j++) {
				if (child[i].pid === parent[j].id) {
					parent[j].children.push(child[i])
				}
			}
		}
        return parent
	},
}
